package com.henu.rescue.controller;


import com.henu.rescue.entity.Complain;
import com.henu.rescue.entity.VO.ComplainVO;
import com.henu.rescue.service.ComplainService;
import com.henu.rescue.service.OrderService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("complain/")
@Api("用户投诉controller")
@Slf4j
public class ComplainController {

    @Autowired
    private ComplainService complainService;

    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    @ApiOperation("模拟用户投诉数据(添加投诉信息)")
    public Result addComplaint(@RequestBody Complain complain) {
        //判断前端数据是否合法
        //判断订单id是否存在
        String orderId = complain.getOrderId();
        Integer integer = orderService.selectById(orderId);
        if(integer != 1){
            return new Result(ResultEnums.PARAMS_ERROR,"工单号不存在");
        }
        Complain complain1 = new Complain();
        complain1.setOrderId(complain.getOrderId());
        complain1.setStandardId(complain.getStandardId());
        Integer count = complainService.selectCountByOrderIdAndStandId(complain1);
        if(count == 1){
            return new Result(ResultEnums.PARAMS_ERROR,"已投诉");
        }
        complainService.addComplain(complain);
        //获取StandardId对应的standardName
        String standardName = complainService.getStandardName(complain.getStandardId());
        ComplainVO complainVO = new ComplainVO();
        BeanUtils.copyProperties(complain, complainVO);
        complainVO.setStandardName(standardName);
        return new Result(ResultEnums.SUCCESS,complainVO);
    }

    @PostMapping("getByOrder")
    @ApiOperation("分页查询投诉信息(可按照orderId或者标准名称查询)")
    public Result getEvaluate(@RequestBody Page<ComplainVO> page) {
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"com_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= complainService.getAllComplainByPage(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @PostMapping("updateStatus/{comId}")
    @ApiOperation("修改投诉状态为已完成处理")
    public Result getEvaluate(@PathVariable Integer comId) {
        Complain complain = complainService.getComplainById(comId);
        //System.out.println(complain);
        if(complain.getReply()==null){
            return new Result(ResultEnums.NO_REPLY);
        }
        Integer integer = complainService.updateStatus(comId);
        if(integer != 1){
            return new Result(ResultEnums.PARM_ERROR);
        }
        return new Result(ResultEnums.SUCCESS);
    }

    @PostMapping("reply")
    @ApiOperation("给用户反馈信息")
    public Result reply(@RequestBody ComplainVO complain) {
        complainService.update(complain);
        return new Result(ResultEnums.SUCCESS);
    }

}
